﻿using System;

namespace ConsoleApp_c1
{
    public class Node<T>
    {   public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }
    public class Genericlist<T>
    {
        private Node<T> head;
        private Node<T> tail;
        public Genericlist()
        {
            tail = head = null;
        }
        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }
        public void ForEach(Action<T> action)
        {
            for(Node<T> node = head; node != null; node = node.Next)
            {
                action(node.Data);
            }
        }
    }
    class Program
    {   
        static void Main(string[] args)
        {   //整型List
            Genericlist<int> intlist = new Genericlist<int>();
            int max=0, min=0, num = 0;
            for (int x = 0; x < 10; x++)
            {
                intlist.Add(x);
            }
            Console.WriteLine(" 整型链表：");
            Console.WriteLine(" 打印链表元素：");
            intlist.ForEach(s => Console.Write(" " + s));
            intlist.ForEach(s => { if (max < s) max = s; });
            Console.WriteLine("\n 最大值为："+max);
            intlist.ForEach(s => { if (min > s) min = s; });
            Console.WriteLine(" 最小值为："+min);
            intlist.ForEach(s => num+=s);
            Console.WriteLine(" 求和："+num);
            //字符串型List
            Genericlist<string> strlist = new Genericlist<string>();
            for(int x = 0; x < 10; x++)
            {
                strlist.Add("str" + x);
            }
            Console.WriteLine(" 字符串型链表：");
            Console.WriteLine(" 打印链表元素：");
            strlist.ForEach(s => Console.Write(" " + s));
        }
    }
}
